import time
import datetime
# import matplotlib.pyplot as plt
# 计算两个日期相差天数，自定义函数名，和两个日期的变量名。
from array import array

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure


def Caltime(date1, date2):
    # %Y-%m-%d为日期格式，其中的-可以用其他代替或者不写，但是要统一，同理后面的时分秒也一样；可以只计算日期，不计算时间。
    # date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")
    # date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")
    date1 = time.strptime(date1, "%m/%d/%Y")
    date2 = time.strptime(date2, "%m/%d/%Y")
    # 根据上面需要计算日期还是日期时间，来确定需要几个数组段。下标0表示年，小标1表示月，依次类推...
    # date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])
    # date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])
    date1 = datetime.datetime(date1[0], date1[1], date1[2])
    date2 = datetime.datetime(date2[0], date2[1], date2[2])
    # 返回两个变量相差的值，就是相差天数
    return (date2 - date1).days


def Caltime2(date1, day):
    # %Y-%m-%d为日期格式，其中的-可以用其他代替或者不写，但是要统一，同理后面的时分秒也一样；可以只计算日期，不计算时间。
    # date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")
    # date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")
    date = datetime.datetime.strptime(date1, "%m/%d/%Y")
    # 根据上面需要计算日期还是日期时间，来确定需要几个数组段。下标0表示年，小标1表示月，依次类推...
    # date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])
    # date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])
    # date1 = datetime.datetime(date1[0], date1[1], date1[2])
    # 返回两个变量相差的值，就是相差天数
    delta = datetime.timedelta(days=day)
    # return (date + delta).strftime("%m/%d/%Y")
    return (date + delta).strftime("%Y-%m-%d")


def Caltime3(day):
    return Caltime2("01/01/1970", day)


def datapre(filename):
    key = []
    value = []

    f = open(filename)
    for line in f:
        (a, b) = line.strip().split("#")
        (a, b) = (int(Caltime('01/01/1970', a)), float(b) * 0.02831685)
        key.append(a)
        value.append(b)
    # print(len((int)(max(value)).__str__()))
    # print((key, value))
    return (key, value)


def dataprePlus(filename, lens=-1):
    (key, value) = datapre(filename)
    maxvaluelen = len((int)(max(value)).__str__())
    n = 1
    for i in range(maxvaluelen):
        n = n * 10
    value = [round(index / n, 8) for index in value]
    return (key[:lens], value[:lens])


def dataN(filename):
    (key, value) = datapre(filename)
    maxvaluelen = len((int)(max(value)).__str__())
    n = 1
    for i in range(maxvaluelen):
        n = n * 10
    return n


def dataBehind(x, y):
    x = [Caltime3(int(index)) for index in x]
    y = [(index if index > 0 else 0) * dataN('../database/A.txt') for index in y]
    return (x, y)


def picture(x, y,
            color="blue",
            linewidth=1.0,
            linestyle="-",
            title="王卓琪",
            label='Y = F(X)',
            label_x='X',
            label_y='Y',
            wangge=False,
            sandian=True):
    plt.figure()
    plt.subplot(111)
    # fig.suptitle('No axes on this figure')

    # 正常显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    # 绘制颜色为蓝色、宽度为 1 像素的连续曲线 y1
    plt.plot(x, y, color=color, linewidth=linewidth, linestyle=linestyle, label=label)

    if sandian:
        plt.plot(x, y, 'ro')

    plt.xlabel(label_x)
    plt.ylabel(label_y)
    plt.title(title)

    plt.legend(loc="upper left")

    # # 设置横轴精准刻度
    # plt.xticks([-1, 0, 1, 2, 3, 4, 5, 6],
    #            ["-1m", "0m", "1m", "2m", "3m", "4m", "5m", "6m"])
    # # 设置纵轴精准刻度
    # plt.yticks([-2, 0, 2, 4, 6, 8, 10],
    #            ["-2m", "0m", "2m", "4m", "6m", "8m", "10m"])
    plt.grid(wangge)  ##增加格点
    plt.axis('tight')  # 坐标轴适应数据量 axis 设置坐标轴

    # plt.show()


def picturePlus(train_x, train_y, test_x, test_y,
                color="blue",
                linewidth=1.0,
                linestyle="-",
                title="王卓琪",
                label_train='Y = train(X)',
                label_test='Y = test(X)',
                label_x='X',
                label_y='Y',
                wangge=False,
                sandian=True):
    plt.figure()
    plt.subplot(111)
    # fig.suptitle('No axes on this figure')

    # 正常显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    # 绘制颜色为蓝色、宽度为 1 像素的连续曲线 y1
    plt.plot(train_x, train_y, color=color, linewidth=linewidth, linestyle=linestyle, label=label_train)
    plt.plot(test_x, test_y, color=color, linewidth=linewidth, linestyle=linestyle, label=label_test)

    if sandian:
        plt.plot(train_x, train_y, 'ro')
        plt.plot(test_x, test_y, 'ro')

    plt.xlabel(label_x)
    plt.ylabel(label_y)
    plt.title(title)

    plt.legend(loc="upper left")

    # # 设置横轴精准刻度
    # plt.xticks([-1, 0, 1, 2, 3, 4, 5, 6],
    #            ["-1m", "0m", "1m", "2m", "3m", "4m", "5m", "6m"])
    # # 设置纵轴精准刻度
    # plt.yticks([-2, 0, 2, 4, 6, 8, 10],
    #            ["-2m", "0m", "2m", "4m", "6m", "8m", "10m"])
    plt.grid(wangge)  ##增加格点
    plt.axis('tight')  # 坐标轴适应数据量 axis 设置坐标轴

    # plt.show()


def pictureMax(x, y, train_x, train_y, test_x, test_y, test_x_tensor, predictive_y_for_testing):
    (x, y) = dataBehind(x, y)
    picture(x, y)
    print("train_y",train_y)
    (train_x, train_y) = dataBehind(train_x, train_y)
    print("predictive_y_for_testing",predictive_y_for_testing)
    (test_x, test_y) = dataBehind(test_x, predictive_y_for_testing)
    picturePlus(train_x, train_y, test_x, test_y)

    print("test_y",test_y)
    # plt.plot(test_x, predictive_y_for_testing, 'm--', label='pre_cos_tst')

    plt.show()
